#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"

SLTNode* SLTBuyNode(SLTDataType x)
{
	SLTNode* newnode =(SLTNode*) malloc(sizeof(SLTNode));
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

void SLTPrint(SLTNode* phead)
{
	SLTNode* pcur = phead;
	while (pcur)
	{
		printf("%d->", pcur->data);
		pcur = pcur->next;
	}
	
	printf("NULL\n");
}

void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	SLTNode* newnode = SLTBuyNode(x);
	SLTNode* pcur = *pphead;
	//phead == NULL
	if (*pphead == NULL)
	{
		*pphead = newnode;
		return;
	}
	//phead != NULL
	while (pcur->next)
	{
		pcur = pcur->next;
	}
	pcur->next = newnode;
}

void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	SLTNode* newnode = SLTBuyNode(x);
	if (*pphead != NULL)
	{
		newnode->next = *pphead;
		*pphead = newnode;
	}
	else
	{
		*pphead = newnode;
	}
}

void SLTPopBack(SLTNode** pphead)
{
	assert(*pphead);
	SLTNode* pcur = *pphead;
	if (pcur->next == NULL)
	{
		free(pcur);
		*pphead = NULL;
		return;
	}
	while (pcur->next->next)
	{
		pcur = pcur->next;
	}
	free(pcur->next);
	pcur->next = NULL;
}

void SLTPopFront(SLTNode** pphead)
{
	assert(*pphead);
	SLTNode* pcur = *pphead;
	if (pcur->next)
	{
		*pphead = pcur->next;
		free(pcur);
		pcur = NULL;
	}
	else
	{
		free(pcur);
		*pphead = NULL;
	}
	
}